﻿using Microsoft.UI;
using Microsoft.UI.Windowing;
using Windows.Graphics;
using WinRT.Interop;
using Microsoft.UI.Xaml;
using Windows.Foundation;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace LifeGame2.WinUI;

/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
public partial class App : MauiWinUIApplication
{
	/// <summary>
	/// Initializes the singleton application object.  This is the first line of authored code
	/// executed, and as such is the logical equivalent of main() or WinMain().
	/// </summary>
	public App()
	{
		this.InitializeComponent();
	}

	protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();

    protected override void OnLaunched(LaunchActivatedEventArgs args) {
        base.OnLaunched(args);

        var elementHandler = Application.Windows[0].Handler;
        if (elementHandler != null) {
            var currentWindow = elementHandler.PlatformView;
            IntPtr windowHandle = WindowNative.GetWindowHandle(currentWindow);
            WindowId windowId = Win32Interop.GetWindowIdFromWindow(windowHandle);

            AppWindow appWindow = AppWindow.GetFromWindowId(windowId);
            appWindow.Resize(new SizeInt32(_width, _height));
            appWindow.Changed += AppWindow_Changed;
        }
    }

    private static int _width = 1440;
    private static int _height = 780;
    private void AppWindow_Changed(AppWindow sender, AppWindowChangedEventArgs args) {
        if (args.DidSizeChange) {
            /*sender.Resize(sender.Size.Height != _height     // Height Changed
                ? new SizeInt32((int) ((sender.Size.Height - 30) * 1.25),
                    sender.Size.Height)
                : new SizeInt32(sender.Size.Width,          // Width Changed
                    (int) (sender.Size.Width * 0.80) + 30));*/

            //TODO
            // WARNING: These settings are for 1920 * 1080 DPI

            // TitleBar's height is 30;
            // TaskBar's height is 40;

            // Display TaskBar;
            
            // 1080 - 30 - 40 = 1010
            // 1920 : 1010 =  1.9001
            // Or we use the "10" (1010 - 1000 = 10), divide it into 2 "5"s as Top and Bottom White Space
            
            if (sender.Size.Height <= 1040) {
                // 1500 : 1000 = 3 : 2;
                // 1920 - 1000 = 420
                // 420 / 1500 = 0.28

                // Check Height Change First Prioritily
                if (sender.Size.Height != _height) {
                    sender.Resize(new SizeInt32((int)((sender.Size.Height - 30) * 1.92), sender.Size.Height));
                    //sender.Size.Width = (sender.Size.Height - 30) * 1.92;
                }else {
                    sender.Resize(new SizeInt32(sender.Size.Width, (int)(sender.Size.Width / 1.92 + 30)));
                    //sender.Size.Height = sender.Size.Width / 1.92 + 30;
                }
            /*}else {
                // NOT Display TaskBar;
                sender.Size.Width = 1920;
                sender.Size.Height = 1080;*/
            } else {
                sender.Resize(new SizeInt32(1920, 1040));
                //sender.Size.Height = 1040;
                //sender.Size.Width = 1920;
            }

            //   _height = sender.Size.Height;
            //   _width = sender.Size.Width;

            // if args.DidSizeChange == true, do following
            _width = sender.Size.Width;
            _height = sender.Size.Height;
        }
        
    }
}

